package com.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author Wang Chen Peng
 * @date 2018/12/24
 * describe:
 */
public class WocketServiceInit extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline channelPipeline = socketChannel.pipeline();
        //基于http协议的编解码器
        channelPipeline.addLast("HttpServerCodec", new HttpServerCodec());
        //对写大数据流的支持
        channelPipeline.addLast("ChunkedWriteHandler", new ChunkedWriteHandler());
        //对http message进行聚合器 里面包括request response 传入一个最大消息的长度 几乎都会用到此
        channelPipeline.addLast("HttpObjectAggregator",new HttpObjectAggregator(1024*64));

        //==================以上是对http协议的支持===============

        /**
         * web socket服务器处理的协议 用于指定给客户端连接访问的路由 : /web-socket
         * 此handler会处理一些繁重复杂的事
         * 例如 握手动作 心跳动作 关闭等
         * 对于web socket来讲 都是以frames进行传输的 不同的数据类型对应的frames也不同
         */
        channelPipeline.addLast("WebSocketServerProtocolHandler", new WebSocketServerProtocolHandler("/ws"));
        //自定义的handler
        channelPipeline.addLast("Custom", new ChatHandler());
    }
}
